package cn.jit.ssm.web.controller;

import cn.jit.ssm.annotation.SecureValid;
import cn.jit.ssm.beans.entity.JitAccount;
import cn.jit.ssm.constant.MethodType;
import cn.jit.ssm.service.AccountService;
import cn.jit.ssm.support.BindingResultErrorExtraction;
import cn.jit.ssm.support.JSONReturn;
import cn.jit.ssm.web.controller.support.BaseController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.Map;

/**
 * 用户处理类
 */
@Controller
public class AccountController extends BaseController {
    final Logger logger = LoggerFactory.getLogger(AccountController.class);

    @Autowired
    private AccountService accountService;

    @RequestMapping(value = "/account/all")
    @ResponseBody
    @SecureValid(code = "01001", desc = "账号管理-查找", type = MethodType.FIND)
    public JSONReturn findAll() {
        return JSONReturn.build(true, "成功");
    }

    @RequestMapping(value = "/account")
    @ResponseBody
    @SecureValid(code = "01002", desc = "角色管理-查找", type = MethodType.FIND)
    public JSONReturn findOne() {
        return JSONReturn.build(false, "失败");
    }


    @RequestMapping(value = "findAccountInfo")
    @ResponseBody
    public JSONReturn findAccountInfo(HttpSession httpSession) {
        return accountService.findAccountInfo("account");
    }

    /**
     * validation demo
     * @param account
     * @param result
     * @return
     */
    @RequestMapping(value = "account/add",method = RequestMethod.POST)
    @ResponseBody
    public JSONReturn addAccountInfo(@Valid JitAccount account, BindingResult result) {
        if(result.hasErrors()){
            Map<String, String> map = BindingResultErrorExtraction.putErrorsToMap(result);
            return JSONReturn.buildFailure(map);
        }
        return JSONReturn.buildSuccess(account);
    }

}